package cn.ibobo.lease.common.util;

//import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

//import org.apache.commons.httpclient.HttpClient;
//import org.apache.commons.httpclient.HttpStatus;
//import org.apache.commons.httpclient.NameValuePair;
//import org.apache.commons.httpclient.methods.PostMethod;

/**
 * Description: 获取手机号验证码工具类 <br /> 
 * Project: meirenhui <br />
 * ClassName: VerifyCode <br />
 * Copyright: Copyright (c) 2017 deshang<br />
 * @author 贾梦莹
 * @version 1.0 2017年11月25日上午11:09:47
 */
public class VerifyCode {

	/**
	 * 获取短信验证码
	 * @author 贾梦莹
	 * @createDate 2018年1月5日上午11:09:33
	 * @param mobile
	 * @return
	 */
	public static Integer getVerifyCode(String mobile, HttpServletRequest request) {
//		HttpClient httpClient = new HttpClient();
//		Integer code = getRandNum(1,999999);
//		try {
//			String url = "http://sms.10690221.com:9011/hy/";
//			String uid = "506991";
//			String auth = Md5Util.md5Encrypt("xacssioo991");
//			String content = java.net.URLEncoder.encode("您的验证码为："+code, "utf-8");
//			PostMethod postMethod = new PostMethod(url);
//			postMethod.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
//			NameValuePair[] data = {
//					new NameValuePair("uid", uid),
//					new NameValuePair("auth", auth),
//					new NameValuePair("encode", "utf-8"),
//					new NameValuePair("mobile", mobile),
//					new NameValuePair("expid", "0"),
//					new NameValuePair("msg", content)};
//			postMethod.setRequestBody(data);
//			int statusCode = httpClient.executeMethod(postMethod);
//			if (statusCode == HttpStatus.SC_OK) {
//				String sms = postMethod.getResponseBodyAsString();
//				System.out.println("result:" + sms);
//			}
//			System.out.println("statusCode=" + statusCode);	 
//		} catch (IOException e) {
//			e.printStackTrace();
//			System.out.println("短信发送过程发生异常!");
//		}
		Integer code = getRandNum(1,999999);
		HttpSession session = request.getSession();
		session.setAttribute("code", code);
		session.setMaxInactiveInterval(60*2);
		return code;
	}
	
	/**
	 * 随机数
	 * @author 贾梦莹
	 * @createDate 2018年1月5日上午10:51:54
	 * @param min
	 * @param max
	 * @return
	 */
	public static int getRandNum(int min, int max) {
	    int randNum = min + (int)(Math.random() * ((max - min) + 1));
	    return randNum;
	}
	
}
